import { createSlice } from "@reduxjs/toolkit";
import { RootState } from "@/modules/store";
import localforage from "localforage";
// 定义命名空间
const namespace = "users";
interface User {
  studentId: string;
  name: string;
  role: string;
  gender: string;
  phone: string;
  email: string;
  department: {
    name: string;
  };
  createdAt: string;
  updatedAt: string;
}

// 定义初始状态
const initialState = {
  data: (await localforage.getItem("HIST-userInfo")) as User,
};

// 创建 slice
const userSlice = createSlice({
  name: namespace,
  initialState,
  reducers: {
    setUserInfo(state, action) {
      state.data = action.payload.data;
    },
  },
});

export const selectUser = (state: RootState) => state.user;
export const { setUserInfo } = userSlice.actions;

export default userSlice.reducer;
